package com.kaibes.platform.file.server.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaibes.core.base.util.StringUtils;
import com.kaibes.platform.file.server.FileClient;
import com.kaibes.platform.file.server.FileClientService;
import com.kaibes.web.admin.AdminController;
import com.kaibes.web.api.ApiLink;

@AdminController
@RequestMapping("/admin/file")
public class FileServerController {

    @Autowired
    private FileClientService fileAesService;
    
    @ApiLink(value = "客户端设置", validated = false)
    @GetMapping("client")
    public String fileClientSetting(@RequestAttribute("userId") Integer userId, Model model) {
        FileClient fileClient = fileAesService.findByUserId(userId);
        if (fileClient == null) {
            fileClient = createClient(userId);
        }
        model.addAttribute("fileClient", fileClient);
        return "file-client";
    }
    
    private FileClient createClient(Integer userId) {
        FileClient fileClient = new FileClient();
        fileClient.setAesKey(StringUtils.random(16));
        fileClient.setAesIv(StringUtils.random(16));
        fileClient.setUserId(userId);
        fileAesService.save(fileClient);
        return fileClient;
    }
    
}
